package com.mikey.callable;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

/**
 * @author :Mikey
 * @description :
 * @create :2022-09-20 16:44:00
 */
//函数接口，可以使用lamda表达式
public class CallableTest {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        // new Thread(new Runnable() {}).start()
        // new Thread(new FutureTask<>() {}).start()
        // new Thread(new FutureTask<>(callable) {}).start()
        new Thread().start();//怎么启动callcable，
        MyThread myThread = new MyThread();
        //适配类
        FutureTask futureTask = new FutureTask<>(myThread);
        new Thread(futureTask,"A").start();
        new Thread(futureTask,"B").start();//结果会被缓存，
        String o = (String)futureTask.get();//get方法可能会产生阻塞，把它放到最后一行，或者使用异步通信
        System.out.println(o);
    }
}
class MyThread implements Callable<String> {

    @Override
    public String call() throws Exception {
        System.out.println("call()");
        return "12345";
    }
}